ig.module('game.main').requires('plugins.plugin', 'impact.game', 'plugins.tween', 'plugins.text', 'game.entities.button', 'game.entities.chair', 'game.entities.card'//, 'impact.debug.debug'
).defines(function(){

    MyGame = ig.Game.extend({
        chairs: null,
        host: null,
        background: new ig.Image('media/bg.jpg'),
        table: new ig.Image('media/table.png'),
        // Load a font
        //font: new ig.Font('media/04b03.font.png'),
        dealing: null,
        dealList: null,
        cardList: null,
        tipText: null,
        pointText: null,
        startButton: null,
        hitButton: null,
        standButton: null,
		resetButton:null,
        init: function(){
            this.host = new Host();
            
            ig.input.bind(ig.KEY.MOUSE1, 'click');
            
            // Initialize your game here; bind keys etc.
            this.chairs = new Array();
            this.chairs.push(this.spawnEntity(Chair, 30, 16, {
                type: Chair.LEFT_TOP,
                seat: 1
            }));
            this.chairs.push(this.spawnEntity(Chair, 30, 580, {
                type: Chair.LEFT_BOTTOM,
                seat: 2
            }));
            this.chairs.push(this.spawnEntity(Chair, 830, 16, {
                type: Chair.RIGHT_TOP,
                seat: 3
            }));
            this.chairs.push(this.spawnEntity(Chair, 830, 580, {
                type: Chair.RIGHT_BOTTOM,
                seat: 4
            }));
            
            this.tipText = new ig.Text('', 'white', "bold 1em Verdana");
            this.tipText.baseLine = "middle";
            this.tipText.align = "left";
            
            this.pointText = new ig.Text('', 'white', "bold 1em Verdana");
            this.pointText.baseLine = "middle";
            this.pointText.align = "left";
            
			this.resetButton = this.spawnEntity(Button,1024-220,808,{
				text:"重启游戏",
				click:function(){
					ig.game.host.command('RESTART');
				},
				enabled:true
			});
            this.startButton = this.spawnEntity(Button, 1024 - 220, 748, {
                text: "开始",
                click: function(){
                    ig.game.host.command('START');
                },
                enabled: false
            });
            this.hitButton = this.spawnEntity(Button, 1024 - 220 * 2, 748, {
                text: "拿牌",
                click: function(){
                    ig.game.host.command("HIT");
                },
                enabled: false
            });
            this.standButton = this.spawnEntity(Button, 1024 - 220 * 2, 808, {
                text: "停牌",
                click: function(){
                    ig.game.host.command("STAND");
                },
                enabled: false
            });
        },
        
        update: function(){
            // Update all entities and backgroundMaps
            this.parent();
            
            for (var i = 0; i < ig.Tween.tweens.length; i++) 
                ig.Tween.tweens[i].update();
            
            // Add your own, additional update code here
            var temp = false;
            for (var i = 0; i < this.entities.length; i++) {
                if (this.entities[i].isMouseEnter) 
                    temp = true;
            }
            if (temp) 
                document.body.style.cursor = 'pointer';
            else 
                document.body.style.cursor = 'default';
            
        },
        
        draw: function(){
            // Draw all entities and backgroundMaps
            //this.parent();
            // Add your own drawing code here
            this.background.draw(0, 0);
            this.table.draw(0, 0);
            
            this.drawEntities();
            
            this.tipText.draw(30, 848);
            this.pointText.draw(30, 788);
            
        },
        updateUserList: function(data){
            for (var dataIndex = 0; dataIndex < data.list.length; dataIndex++) {
                for (var chairIndex = 0; chairIndex < this.chairs.length; chairIndex++) {
                    if (data.list[dataIndex].Seat == this.chairs[chairIndex].seat) {
                        this.chairs[chairIndex].enabled = false;
                        this.chairs[chairIndex].player = data.list[dataIndex];
                    }
                    
                    if (data.list[dataIndex].IsSelf) {
                        this.startButton.enabled = !data.list[dataIndex].IsStart;
                    }
                }
            }
        },
        starting: false,
        startPoker: function(data){
            console.log("---------Start Game---------");
            console.log(data);
            console.log("---------Start Game---------");
            if (this.starting) 
                return;
            else 
                this.starting = true;
            
            
            this.cardList = new Array();
            for (var i = 0; i < 52; i++) {
                var tmp = (i / 10) * 2;
                this.cardList.push(this.spawnEntity(Card, (ig.system.width / 2 - 107.3 / 2) - tmp, (ig.system.height / 2 - 160 / 2) - tmp));
            }
            
            for (var i = 0; i < this.chairs.length; i++) {
                this.chairs[i].enabled = false;
            }
            
            this.dealList = data.info.DealList;
            if (this.dealList != null && this.dealList.length > 0) {
                this.dealPoker(0, function(){
                    ig.game.host.command("START_COMPLETE");
                });
            }
        },
        dealCardPoss: [{
            seat: 1,
            pos: {
                x: 200,
                y: 160
            }
        }, {
            seat: 2,
            pos: {
                x: 200,
                y: 440
            }
        }, {
            seat: 3,
            pos: {
                x: 620,
                y: 160
            }
        }, {
            seat: 4,
            pos: {
                x: 620,
                y: 440
            }
        }],
        currentDealPokerIndex: 0,
        dealPoker: function(index, dealPokerCompleted){
            this.currentDealPokerIndex = index;
            var deal = this.dealList[index];
            for (var ci = 0; ci < this.chairs.length; ci++) {
                var chair = this.chairs[ci];
                if (chair.seat == deal.Seat) {
                
                    var dealCardPos = this.dealCardPoss[chair.seat - 1];
                    var pos = {
                        x: dealCardPos.pos.x + (chair.cardDataList.length * 30),
                        y: dealCardPos.pos.y
                    };
                    var card = this.cardList.pop();
                    chair.addCard({
                        dealCard: deal.Card,
                        pos: pos,
                        card: card
                    }, function(){
                        if (ig.game.currentDealPokerIndex == ig.game.dealList.length - 1) 
                            dealPokerCompleted();
                        else 
                            ig.game.dealPoker(ig.game.currentDealPokerIndex + 1, dealPokerCompleted);
                    });
                    card.zIndex += index;
                    ig.game.sortEntities();
                }
            }
        },
        wait: function(){
            this.tipText.text = "请选择";
            this.hitButton.enabled = this.standButton.enabled = true;
        },
        waitOther: function(){
            this.tipText.text = "等待其他玩家决策";
            this.hitButton.enabled = this.standButton.enabled = false;
        },
        hit: function(data){
            console.log("---------Hit---------");
            console.log(data);
            console.log("---------Hit---------");
            if (data.result.IsExplode) 
                this.pointText.text = "爆了！";
            else 
                this.pointText.text = data.result.Point + " 点";
            
            this.currentDealPokerIndex++;
            for (var ci = 0; ci < this.chairs.length; ci++) {
                var chair = this.chairs[ci];
                if (chair.seat == data.result.Seat) {
                
                    var dealCardPos = this.dealCardPoss[chair.seat - 1];
                    var pos = {
                        x: dealCardPos.pos.x + (chair.cardDataList.length * 30),
                        y: dealCardPos.pos.y
                    };
                    var card = this.cardList.pop();
                    chair.addCard({
                        dealCard: data.result.Card,
                        pos: pos,
                        card: card
                    }, function(){
                    });
                    card.zIndex += this.currentDealPokerIndex;
                    ig.game.sortEntities();
                }
            }
        },
        endGame: function(data){
            console.log("---------End Game---------");
            console.log(data);
            console.log("---------End Game---------");
            this.tipText.text = '';
            this.pointText.text = '游戏结束';
            
            setTimeout(function(){
                ig.system.setGame(MyGame);
            }, 3000);
        },
		resetGame:function(data){
			ig.system.setGame(MyGame);
		}
    });
    
    
    // Start the Game with 60fps, a resolution of 320x240, scaled
    // up by a factor of 2
    window.onunload = function(){
    	
    };
    
    var game_width = 1024;
    var game_height = 868;
    var game_scale = 0.75;
    var game_fps = 50;
    
    if (ig.ua.mobile) {
        game_fps = 30;
    }

	game_scale = Math.min(($('#container').width() / game_width), ($('#container').height() / game_height));
	
	$(document).ready(function(){
		ig.main('#canvas', MyGame, game_fps, game_width, game_height, game_scale, ig.ImpactSplashLoader);		
	});
    
});
